package com.worthtech.billportal.dao.hibernate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import com.worthtech.billportal.bean.CsAppTerminal;
import com.worthtech.billportal.bean.User;
import com.worthtech.billportal.bean.WtMgrEncKey;
import com.worthtech.billportal.bean.WtMgrMerInfo;
import com.worthtech.billportal.bean.WtTermAuth;
import com.worthtech.billportal.bean.WtTermInfo;
import com.worthtech.billportal.dao.TerminalDAO;
import com.worthtech.billportal.exception.SCException;
import com.worthtech.billportal.util.AbstractManager;

public class TerminalHibernateDAO extends AbstractManager implements TerminalDAO {
	
	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if(perNo==0){
			return this.getHibernateTemplate().find(hql);
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public void save(CsAppTerminal terminal) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(terminal);
	}
	
	public String getSeqId(String tablename){
		return this.getSession().createSQLQuery("SELECT "+tablename+".NEXTVAL FROM dual").list().get(0).toString();
	}

	public boolean update(CsAppTerminal terminal) {
		try {
			this.getHibernateTemplate().update(terminal);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	public String getCount(String tableName, String condition){
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
//		System.out.println("count=" + tmp);
		return tmp;
	}
	public void delete(CsAppTerminal c){
		this.getHibernateTemplate().delete(c);
	}

	public String getSumBySQL(String list, String tableName, String condition) {
		String hql = "select sum(" + list + ") from " + tableName + " "
				+ condition;
		System.out.println("hql1=" + hql);
		List l = this.getSession().createSQLQuery(hql).list();
		if (l.size() == 0) {
			return "0";
		}
		if (l.get(0) == null) {
			return "0";
		}
		String tmp = l.get(0).toString();
		System.out.println("count=" + tmp);
		return tmp;
	}
	public void updateMgrKey(WtMgrEncKey key){
		PreparedStatement ps;
		ResultSet rset = null;
		Connection c = this.getSession().connection();
			try {
				ps = c.prepareStatement("update wt_mgr_enc_key set mgr_sys_index = '"+key.getId().getMgrSysIndex()+"' where mgr_mer_id = '"+key.getId().getMgrMerId()+"' and mgr_term_id = '"+key.getId().getMgrTermId()+"' ");
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public void deletewtinfo(String tablename,String sql){
		String sqldel ="delete from "+tablename+sql;
		System.out.println(sqldel);
		this.getSession().createQuery(sqldel).executeUpdate();
	}
	
	//sim-termid
	public void delTermAuth(WtTermAuth wa){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql="delete from wt_term_auth where term_code='"+wa.getId().getTermCode()+"' and term_simno='"+wa.getId().getTermSimno()+"' and term_date=to_date('"+sdf.format(wa.getId().getTermDate())+"','yyyy/mm/dd hh24:mi:ss') ";
		System.out.println(sql);
		Connection con = this.getSession().connection();
		try {
			PreparedStatement  ps = con.prepareStatement(sql);
			int x = ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
